<?php

/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link
 * @package       Model
 * @since         MaginPHP 1.0
 * @license
 */
?>
<?php

/**
 * @desc: This is comman function Class that used in every where
 * @author aNKIT kHAMBHATA
 */
class Magic_Common_Model {
    /**
     * @author: aNKIT kHAMBHATA
     * @var type 
     */
    private static $_instance ; 
    public static function callMethod(){
        self::$_instance = new self ; 
        return self::$_instance ;
    }
    /**
     * @author: aNKIT kHAMBHATA
     * @desc: Copy  file from source to destination
     * @param $source : source file
     * @param $destination: destination file
     */
    public function uploadFile($source, $destination) {
        $copy = copy($source, $destination);
        if ($copy) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @author: aNKIT kHAMBHATA
     * @desc: This file is used to remove the file
     * @param $file: name of file
     */
    public function removeFile($file) {
        $unlink = @unlink($file);
        if ($unlink) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @author : aNKIT kHAMBHATA
     * @desc : This function user to redirect the page
     * @param: $page: page name on which redirect
     */
    public function _pageRedirect($page) {
        print "<script>";
        print "location = '$page'";
        print "</script>";
        @header("Location : $page");
    }

    /**
     * @author : aNKIT kHAMBHATA
     * @desc: for encode the data
     * @param: $data : data string
     */
    public function encodeData($data) {
        $str = urlencode($data);
        $str_new = base64_encode($str);
        return $str_new;
    }

    /**
     * @author : aNKIT kHAMBHATA
     * @desc : Data encode
     * @param: $data : string of data
     */
    public function decodeData($data) {
        $str = urldecode($data);
        $str_new = base64_decode($str);
        return $str_new;
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @desc : This function is used to find element and its position
     * from multidimention array
     * @param $needle : multidimention array
     * @param $haystack: value for which you search
     * @return array result 0 /1 and its position in which child array it exsist
     */
    public function inMultiDimentionArray($needle, $haystack) {
        $j = 0;
        $inResult = 0;
        $inPosition = 0;
        foreach ($haystack as $key => $arr) {
            if (is_array($arr)) {
                if (in_array($needle, $arr)) {
                    $inResult = 1;
                    $inPosition = array_search($arr, $haystack);
                    break;
                }
            }
        }
        return array("result" => $inResult, "position" => $inPosition);
    }

    /**
     * @author : aNKIT kHAMBHATA
     * @desc : this Function is used to push element in array after reference element
     * @param $inputArray : Existing Array
     * @param $referenceElement : reference element array 
     * @param $newElement :array of new element 
     * @return multitype:
     */
    function pushElementInArray($inputArray, $referenceElement, $newElement) {
        for ($i = 0; $i < count($referenceElement); $i++) {
            $key = array_search($referenceElement[$i], $inputArray);
            $secondHalf = array_slice($inputArray, $key + 1);
            $firstHalf = array_slice($inputArray, 0, $key + 1);
            $resultArray = array($newElement[$i]);
            $newResult = array_merge($firstHalf, $resultArray, $secondHalf);
            $inputArray = $newResult;
            $newResult = '';
        }
        return ($inputArray);
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @desc : this Function is used to find key or value exist in mulidimentinal array not
     * @param type $needle : key or value
     * @param type $heystack : array
     * @return boolean ture/false 
     */
    public function inMultiArray($needle,$heystack) {
    if (array_key_exists($needle,$heystack) or in_array($needle,$heystack)) {
             return true;
        } else {
            $return = false;
            foreach (array_values($heystack) as $value) {
                if (is_array($value) and !$return) {
                    $return = inMultiArray($needle,$value);
                }
            }
            return $return;
        }
    }
}

?>